<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 5] 5.2 Lexical Scope of Declarations</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:12:32 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch05_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 5<br>Declarations</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch05_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-5-SECT-2">5.2 Lexical Scope of Declarations</A></h2>

<P CLASS=para>
The lexical scope of a declaration determines where the named entity is
a valid identifier. Every declaration is associated with a lexical
level that corresponds to one of the following Java constructs:

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry>Package<br>
<DD>

<P CLASS=para>
The names at this level include all of the non-nested, outer-level class
and interface declarations in files that belong to the same
package as the file that is being compiled. This level also includes non-nested, outer-level
class and interface declarations that are declared <tt CLASS=literal>public</tt>
in other packages.

<DT CLASS=varlistentry><I CLASS=emphasis>.java</I> file<br>
<DD>

<P CLASS=para>
The names at this level include all of the class
and interface declarations in the file, as well as all of the classes
and interfaces that are imported by the file. The names declared
directly in a file are defined from the beginning to the end of
the file. An <tt CLASS=literal>import</tt> statement defines simple
identifiers as synonyms for names that are only fully qualified
with the name of a package. These synonyms for fully qualified names
are defined from the <tt CLASS=literal>import</tt> statement that defines
them to the end of the file.

<p>
<DT CLASS=varlistentry>Class or interface declaration<br>
<DD>

<P CLASS=para>
The names at this level include the names of methods, variables,
and classes or interfaces 
that are declared directly in the class or interface
declaration, as well as names inherited from superclasses or super
interfaces. The names declared in a class or interface are defined
throughout the class or interface.

<p>
<DT CLASS=varlistentry>Method declaration<br>
<DD>

<P CLASS=para>
The names at this level include the formal parameters
of the method. The formal parameters are defined throughout the
method.

<p>
<DT CLASS=varlistentry>Block<br>
<DD>

<P CLASS=para>
The names at this level include the local variables, local classes,
and statement labels declared in the block. Statement labels are
defined throughout a block, while local variables and classes are defined from
their declaration to the end of the block.

<p>
<DT CLASS=varlistentry>A nested block or a <tt CLASS=literal>for</tt> statement<br>
<DD>

<P CLASS=para>
The names at this level include local variables declared in the
initialization of the <tt CLASS=literal>for</tt> statement or the local
variables, classes, and statement labels declared in a nested block. Local
variables declared in the initialization of a <tt CLASS=literal>for</tt>
statement are defined from their declaration to the end of the
<tt CLASS=literal>for</tt> statement. Statement labels are defined
throughout a nested block, while local variables and classes are defined from
their declaration to the end of the nested block.</DL>
<P CLASS=para>
These lexical levels correspond to nested constructs. When
the Java compiler encounters a name in a program, it finds the declaration
for that name by first looking in the lexical level where the name
is encountered. If the compiler does not find the name in that lexical
level, it searches progressively higher lexical levels until it
finds the declaration. If all of the lexical levels are exhausted,
the compiler issues an error message.

<P CLASS=para>
If, however, an
identifier is qualified by a class or package name, the compiler
only searches that lexical level for a declaration.

<P CLASS=para>
<b>References</b>
<A HREF="ch06_01.htm#JLR2-CH-6-SECT-1">Blocks</A>;
<A HREF="ch05_04.htm#JLR2-CH-5-SECT-4">Class Declarations</A>;
<A HREF="ch05_05.htm#JLR2-CH-5-SECT-5">Interface Declarations</A>;
<A HREF="ch07_02.htm#JLR2-CH-7-SECT-2">Packages</A>;
<A HREF="ch05_04.htm#JLR2-CH-5-SECT-4.4.2">Methods</A>;
<A HREF="ch06_07.htm#JLR2-CH-6-SECT-7.3">The for Statement</A>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch05_01.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch05_03.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Naming Conventions</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Object-Orientation Java Style</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
